import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: Themberfue
 * @date: 2025/4/5 11:05
 * @description:
 */
public class RedisHash {
    public static void main(String[] args) {
        try (JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
             Jedis jedis = jedisPool.getResource()) {
            hmgetAndHmset(jedis);
        }
    }

    private static void hmgetAndHmset(Jedis jedis) {
        jedis.flushAll();

        Map<String, String> fv = Map.of("f1", "111", "f2", "222", "f3", "333");
        jedis.hmset("k1", fv);

        List<String> hmgetRes = jedis.hmget("k1", "f1", "f2", "f3", "f4");
        System.out.println(hmgetRes);
    }

    private static void hkeysAndHvals(Jedis jedis) {
        jedis.flushAll();

        Map<String, String> fv = Map.of("f1", "111", "f2", "222", "f3", "333");
        jedis.hset("k1", fv);

        Set<String> hkeysRes = jedis.hkeys("k1");
        System.out.println(hkeysRes);

        List<String> hvalsRes = jedis.hvals("k1");
        System.out.println(hvalsRes);
    }

    private static void hexistsAndHdel(Jedis jedis) {
        jedis.flushAll();

        Map<String, String> fv = Map.of("f1", "111", "f2", "222", "f3", "333");
        jedis.hset("k1", fv);

        boolean hexistsRes = jedis.hexists("k1", "f1");
        System.out.println(hexistsRes);

        long hdelRes = jedis.hdel("k1", "f1", "f2", "f3", "f4");
        System.out.println(hdelRes);
    }

    private static void hsetAndHget(Jedis jedis) {
        jedis.flushAll();

        jedis.hset("k1", "f1", "111");
        Map<String, String> fv = Map.of("f2", "222", "f3", "333");
        jedis.hset("k1", fv);

        String hgetRes = jedis.hget("k1", "f2");
        System.out.println(hgetRes);
    }
}
